package com.huarui.purchaseOrder.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
//import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ParameterAware;
import org.ewaf.framework.action.BaseAction;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.params.Params;
import com.huarui.purchaseOrder.enums.PurchaseFunctionEnum;
//import com.huarui.purchaseOrder.enums.PurchaseOperateMesEnum;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.purchaseOrder.service.PurchaseOrderService;
import com.huarui.task.taskEnum.WorkFlowTaskNameEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
//import com.huarui.util.ConstantUtil;
import com.huarui.util.FuncUtil;
import com.opensymphony.xwork2.ActionContext;

public class PurchaseQueryAction extends BaseAction implements ParameterAware{

	public static String MESS = "mess";
	private static final long serialVersionUID = 5357573225759419406L;
//	private Logger logger = Logger.getLogger(PurchaseQueryAction.class);
	private PurchaseOrderService purchaseOrderService;
	
	private Integer ordersId;
	private Integer start;
	private String status;
	private Map<String, Object> jsonObject;
	private Map<String, Object> mesMap;
	private String conditions;
	private List<Map<String, Object>> purchaseList = new ArrayList<Map<String, Object>>();
	
	/**
	 * ParameterAware接口里调用方法时设置的属性
	 */
	private Map<String, String[]> parameters;

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public List<Map<String, Object>> getPurchaseList() {
		return purchaseList;
	}

	public void setPurchaseList(List<Map<String, Object>> purchaseList) {
		this.purchaseList = purchaseList;
	}

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public Integer getOrdersId() {
		return ordersId;
	}

	public void setOrdersId(Integer ordersId) {
		this.ordersId = ordersId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	
	/**
	 * 单据查询或还原
	 * @return
	 */
	private String toShow() {
		mesMap = purchaseOrderService.loadPurchaseByOrderId(ordersId);
		return MESS;
	}
	
	/**
	 * 在线单据查询或还原
	 * @return
	 */
	public String onlineToShow() {
		return this.toShow();
	}
	
	/**
	 * 非在线单据查询或还原
	 * @return
	 */
	public String notOnlineToShow() {
		return this.toShow();
	}
	
	/**
	 * 重置
	 * @return
	 */
	private String reset() {
		mesMap = new HashMap<String, Object>();
		mesMap.put(Params.PUR_ORDER_STATUS_PARAM, PurOrSaleOrder.STATUS_INITIAL);
		mesMap.put(Params.PUR_LOADED_MESS_KEYS, PurchaseFunctionEnum.LOAD_BUTTONS_TOOL_BAR.getKey());
		return MESS;
	}
	
	/**
	 * 在线重置
	 * @return
	 */
	public String onlineReset() {
		return this.reset();
	}
	
	/**
	 * 非在线重置
	 * @return
	 */
	public String notOnlineReset() {
		return this.reset();
	}
	
	/**
	 * load主表
	 * @return
	 */
	private String getPurchaseById() {
		mesMap = this.purchaseOrderService.getPurchaseById(ordersId);
		
		return MESS;
	}

	public String loadOnlineOrders() {
		return getPurchaseById();
	}
	
	public String loadNotOnlineOrders() {
		return getPurchaseById();
	}
	
	/**
	 * load页面上的采购订单所有明细数据
	 * @return
	 */
	private String getPurchaseOrderDetailsByOrderId() {
		
		List<PurOrSaleOrderDetail> orderDetails = purchaseOrderService
				.getPurchaseOrderDetailsByOrderId(ordersId);

		mesMap = new HashMap<String, Object>();
		mesMap.put(Params.PUR_DETAILS_PARAM, orderDetails);
		
		return MESS;
	}

	public String loadOnlineDetails() {
		return getPurchaseOrderDetailsByOrderId();
	}
	
	public String loadNotOnlineDetails() {
		return getPurchaseOrderDetailsByOrderId();
	}
	
	/**
	 * 根据订单编号查询所有明细(按明细方式浏览)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getPurDetailsInfoByComplex() {
		Map<String, Object> paramsMap = null;
		if(conditions != null) {
			JSONObject jo = JSONObject.fromObject(conditions);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			ActionContext ctx = ActionContext.getContext();
			paramsMap = FuncUtil.converToParamMap(ctx.getParameters());
		}
		
		paramsMap.put("start", start==null?0:start);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		mesMap = this.purchaseOrderService.getPurDetailsInfoByComplex(paramsMap);
		
		return MESS;
	}
	
	/**
	 * 在线明细方式浏览
	 * @return
	 */
	public String listOnlineOrderDetails() {
		return getPurDetailsInfoByComplex();
	}
	
	/**
	 * 非在线明细方式浏览
	 * @return
	 */
	public String listNotOnlineOrderDetails() {
		return getPurDetailsInfoByComplex();
	}
	
	/**
	 * 根据订单编号查询主单(按单头方式浏览)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getPurInfoByComplex() {
		/**
		 * TODO using parameters
		 */
//		ActionContext ctx = ActionContext.getContext();
//		Map<String, Object> paramsMap = FuncUtil.converToParamMap(ctx
//				.getParameters());
		Map<String, Object> paramsMap = null;
		if(conditions != null) {
			JSONObject jo = JSONObject.fromObject(conditions);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			ActionContext ctx = ActionContext.getContext();
			paramsMap = FuncUtil.converToParamMap(ctx.getParameters());
		}
		
		paramsMap.put("start", start==null?0:start);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		mesMap = this.purchaseOrderService.getPurInfoByComplex(paramsMap);
		
		return MESS;
	}
	
	/**
	 * 在线主单方式浏览
	 * @return
	 */
	public String listOnlineOrders() {
		return getPurInfoByComplex();
	}
	
	/**
	 * 非在线主单方式浏览
	 * @return
	 */
	public String listNotOnlineOrders() {
		return getPurInfoByComplex();
	}
	
	/**
	 * 查询订单主表和子表信息,子表信息关连宝典信息 xu
	 * 
	 * @return
	 */
	public String getOrdersInfo() {
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyId = sessionMessage.getCompanyId();
		jsonObject = new HashMap<String, Object>();
		PurOrSaleOrder order=purchaseOrderService.getPurchaseByIdAndSellerId(ordersId,companyId);
		if(order==null){
			jsonObject.put("success", false);
			
		}else{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ordersId", ordersId);
			map.put("varGroup", DictionaryEnum.GROUP_SOURCE.getGroup());
			List<Map<String, Object>> list = purchaseOrderService
					.getOrderItems(map);
			jsonObject.put("success", true);
			jsonObject.put("main", order);
			jsonObject.put("item", list);
			
		}

		return JSON;

	}
	
	
	@SuppressWarnings("unchecked")
	public String getAvailabeOrders(){

		if( StringUtils.isNotEmpty(conditions) ) {
			JSONObject jo = JSONObject.fromObject(conditions);
			mesMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		} else {
			mesMap = new HashMap<String, Object>();
		}
		
		mesMap.put("start", start==null ? 0 : start);
		mesMap.put("pageSize", Params.PAGE_SIZE);

		mesMap = this.purchaseOrderService.getAvailabeOrdersMess(mesMap);
		
		return MESS;
	}
	
	@Override
	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
	}
	

}
